package com.ganbo.ms.netty.simple.utils;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;

import static io.netty.util.internal.StringUtil.NEWLINE;

/**
 * ByteBuf打印工具类,便于查看ByteBuf里面的内容和大致结构
 *
 * @author ganbo
 * @date 2022/12/7 22:27
 */
public class ByteBufLogUtils {

    public static void log(ByteBuf buffer) {
        int length = buffer.readableBytes();
        int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
        StringBuilder buf = new StringBuilder(rows * 80 * 2)
                .append("read index:").append(buffer.readerIndex())
                .append(" write index:").append(buffer.writerIndex())
                .append(" capacity:").append(buffer.capacity())
                .append(NEWLINE);
        ByteBufUtil.appendPrettyHexDump(buf, buffer);
        System.out.println(buf.toString());
    }

}
